package app.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import database.local.Business;
import database.local.Deal;
import database.local.PicturesUtil;
import database.local.Utils;

public class DealsListArrayAdapter extends BaseAdapter {
	
	private static List<Deal> dealsList;
	private LayoutInflater mInflater;
	private Context context;
	private Business business;

	public DealsListArrayAdapter (Context context, List<Deal> results, Business b){
		dealsList = results;
		mInflater = LayoutInflater.from(context);
		this.context = context;
		business = b;
		
	}
	public int getCount() {
		return dealsList.size();
	}

	public Object getItem(int position) {
		return dealsList.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
		
		if (convertView == null){
			convertView = mInflater.inflate(R.layout.list_row_layout, null);
			holder = new ViewHolder();
			
			holder.title = (TextView) convertView.findViewById(R.id.row_title_text);
			holder.rateBar = (RatingBar) convertView.findViewById(R.id.row_rate_ratingbar);
			holder.distance = (TextView) convertView.findViewById(R.id.row_distance_text);
			holder.name = (TextView) convertView.findViewById(R.id.row_business_name_text);
			holder.moneyBar = (RatingBar) convertView.findViewById(R.id.row_moeny_ratingbar);
			holder.image = (ImageView) convertView.findViewById(R.id.row_deal_image);
			holder.address = (TextView) convertView.findViewById(R.id.row_address_text);
			
			convertView.setTag(holder);
		}
		else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		holder.address.setText(business.getFullAddress());				
		holder.rateBar.setRating(dealsList.get(position).getRate());
		holder.name.setText("At " +business.getName());
		holder.moneyBar.setRating(dealsList.get(position).getCostLevel());
		holder.distance.setText(Utils.getDistanceStr(business.getDistance()));
		holder.image.setImageDrawable(PicturesUtil.getDealPicSmall(dealsList.get(position), context));
		holder.title.setText(dealsList.get(position).getShortName());
		
		return convertView;
	}
	static class ViewHolder {
		TextView title, name, distance, address;
		RatingBar rateBar, moneyBar;
		ImageView image;
	}

}
